//
//  LoadingView.swift
//  加载 View
//
//  Created by mini on 2025/4/28.
//

import Foundation
import UIKit


class LoadingView: UIView {
    
    // MARK: - lazy
    lazy var imageLoading: UIImageView = {
        let result = UIImageView()
        result.image = UIImage(named: "icon_loading")
        result.widthHeight = XY(32, 32)
        return result
    }()
    
    // MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT)
        self.addSubViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func addSubViews() {
        self.addSubview(self.imageLoading)
        self.imageLoading.centerXCenterY = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)
    }
    
    func hideLoading() {
        self.stopAnimate()
        self.removeFromSuperview()
    }
    
    func beginAnimate() {
        self.stopAnimate()
        let animate = CABasicAnimation(keyPath: "transform.rotation")
        animate.toValue = NSNumber(value: Double.pi * 2)
        animate.repeatCount = .greatestFiniteMagnitude
        animate.duration = 2
        animate.isRemovedOnCompletion = true
        self.imageLoading.layer.add(animate, forKey: "animated")
    }
    
    func stopAnimate() {
        self.imageLoading.layer.removeAllAnimations()
    }
   
    
}
